﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FrameWork.Lib;
using FrameWork.WinUI.Common;

namespace REVISIONMAX30
{
    public partial class frmSourceDirectory : Form
    {
        public frmSourceDirectory()
        {
            InitializeComponent();

            mListViewSorter = new FrameWork.WinUI.Common.ListViewSorter(lstFiles);
        }

        private FrameWork.WinUI.Common.ListViewSorter mListViewSorter = null;
        public UIManager UIManager { get; set; }
        public DataObjects.SourceDirectory sourceDirectory { get; set; }
        public DataObjects.Group Group { get; set; }

        public void View(DataObjects.SourceDirectory pChildObject, DataObjects.Group pParentObject)
        {
      
            this.sourceDirectory = pChildObject;
            this.Group = pParentObject;
            this.UpdateViewer();
            base.ShowDialog();
        }

        public Guid DataObjectGUID
        {
            get { return this.sourceDirectory.DataObjectGUID; }
        }

        private void UpdateObject()
        {
            this.sourceDirectory.DirectoryName = this.txtDirectoryName.Text;
            this.sourceDirectory.ScanDirectoryRecusivly = this.chkScanDirectoryRecusivly.Checked;
        }

        private void UpdateViewer()
        {
            this.txtDirectoryName.Text = this.sourceDirectory.DirectoryName;
            this.chkScanDirectoryRecusivly.Checked = this.sourceDirectory.ScanDirectoryRecusivly.BoolValue();

            UpdateFiles();
        }

        private void UpdateFiles()
        {
            lstFiles.Items.Clear();

            if (sourceDirectory.DirectoryExists())
            {
                foreach (var file in this.sourceDirectory.GetFiles())
                {
                    var tParseFileNameArray = this.sourceDirectory.ParseFileName(file);
                    var tName = System.IO.Path.GetFileName(file);

                    var tItem = lstFiles.Items.Add(tName);

                    if (tParseFileNameArray != null)
                    {
                        tItem.SubItems.Add(tParseFileNameArray[1]);
                        tItem.SubItems.Add(tParseFileNameArray[0]);
                        tItem.SubItems.Add(tParseFileNameArray[2]);
                        if (tParseFileNameArray.Length >= 4)
                            tItem.SubItems[0].Text = tParseFileNameArray[3];
                        tItem.SubItems.Add(file);
                    }
                    else
                    {
                        tItem.SubItems.Add("");
                        tItem.SubItems.Add("");
                        tItem.SubItems.Add("");
                        tItem.SubItems.Add(file);
                    }
                }
            }

            lstFiles.AutoResize();
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            this.Save(true);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            this.Save(false);
        }

        public void Save(bool pApply)
        {
            UpdateObject();

            if (!Group.SourceDirectoryList.Exists(t => t.DataObjectGUID == this.DataObjectGUID))
            {
                Group.SourceDirectoryList.Add(this.sourceDirectory);
            }
            else
            {
                this.sourceDirectory.Updated = DateTime.Now;
                this.Group.SourceDirectoryList.RemoveAll(t => t.DataObjectGUID == this.sourceDirectory.DataObjectGUID);
                this.Group.SourceDirectoryList.Add(this.sourceDirectory);
            }
 

            if (!pApply)
            {
                base.Close();
            }
            else
            {
                UpdateViewer();
            }
        }

        private void txtDirectoryName_TextChanged(object sender, EventArgs e)
        {
            this.Text = "Directory Name: " + this.txtDirectoryName.Text;
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            var tfrmBrowseDirectory = new FrameWork.WinUI.frmBrowseDirectory();

            var tDir = tfrmBrowseDirectory.View(this.txtDirectoryName.Text);

            if (tDir != null)
            {
                this.sourceDirectory.DirectoryName = tDir;

                UpdateViewer();
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            UpdateFiles();
        }

        private void chkScanDirectoryRecusivly_CheckedChanged(object sender, EventArgs e)
        {
            UpdateObject();
            UpdateFiles();
        }

  
    }
}
